<template>
  <div class="material">
    <el-card shadow="never" class="container">
      <div slot="header">
        <el-upload
          ref="uploadMaterialImg"
          action="http://leju.bufan.cloud/lejuAdmin/material/uploadFileOssSave"
          :headers="token"
          :on-success="handleUploadSuccess"
          :before-upload="handleBeforeUpload"
          accept=".png,.jpeg,.gif,.jpg,.webp"
          :show-file-list="false"
        >
          <el-button size="mini" type="primary">点击上传素材</el-button>

          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </div>
      <el-row :gutter="10">
        <el-col
          v-for="item in materialList"
          :key="item.id"
          :span="5"
          :offset="0"
          class="col"
        >
          <el-card shadow="always">
            <div slot="header">
              <span>创建时间: {{ item.createTime }}</span>
            </div>
            <el-image
              class="image"
              :src="item.ossUrl"
              :preview-src-list="imgList"
            />
            <div class="card-bottom">
              <el-button
                type="danger"
                size="mini"
                class="del"
                @click="handleDel(item)"
              >删除</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div class="footer">
        <el-pagination
          :current-page.sync="page.start"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalNum"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script>
import { findMaterialByPage, delMaterial } from '@/api/content/materialList'
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      materialList: [],
      page: {
        start: 1,
        limit: 10,
        totalNum: 0
      }
    }
  },
  computed: {
    // 预览大图列表
    imgList() {
      return this.materialList.map((item) => {
        return item.ossUrl
      })
    },
    // 登录token
    token() {
      return { token: getToken() }
    }
  },
  created() {
    this.getMaterialList()
  },
  methods: {
    // 获取素材列表
    async getMaterialList() {
      const { success, data, message } = await findMaterialByPage(
        this.page.start,
        this.page.limit
      )
      if (success) {
        this.materialList = data.rows
        this.page.totalNum = data.total
      } else {
        this.$message.error(message)
      }
    },
    // 上传素材之前,检查素材是否过大
    handleBeforeUpload(file) {
      var imgReg = /^image\/(jpeg|jpg|png|gif|webp)$/
      const isImg = imgReg.test(file.type)
      const isLt1Dot5M = 1.5 * 1024 * 1024 > file.size
      if (!isImg) {
        this.$message.error('上传头像图片只能是 jpeg|jpg|png|gif|webp 格式!')
      }
      if (!isLt1Dot5M) {
        this.$message.error('上传头像图片大小不能超过 1.5MB!!!')
      }
      return isLt1Dot5M && isImg
    },
    // 上传素材成功
    handleUploadSuccess() {
      this.$refs.uploadMaterialImg.clearFiles()
      this.getMaterialList()
    },
    // 删除素材
    handleDel(item) {
      this.$confirm('此操作将永久删除该素材, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      })
        .then(async() => {
          const { success, message } = await delMaterial(item.id)
          if (success) {
            this.$message.success('删除成功')
            // 刷新列表
            this.getMaterialList()
          } else {
            this.$message.error(message)
          }
        })
        .catch(() => {
          this.$message.info('您取消了操作')
        })
    },
    sizeChange(newSize) {
      this.page.limit = newSize
      this.page.start = 1
      this.getMaterialList()
    },
    currentChange(newVal) {
      this.page.start = newVal
      this.getMaterialList()
    }
  }
}
</script>
<style lang="scss" scoped>
.material {
  .container {
    margin: 30px;
    .col {
      padding-left: 10px;
      padding-right: 10px;
      margin-bottom: 20px;
      width: 20%;
      .card-bottom {
        display: flex;
        justify-content: flex-end;
        margin-top: 10px;
        .del {
          width: 90px;
        }
      }
    }
    .image {
      width: 260px;
      height: 250px;
    }
    .footer {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
